package config

import (
	"github.com/spf13/viper"
)

type ServerConfig struct {
	Port      int    `mapstructure:"port"`
	JWTSecret string `mapstructure:"jwt_secret"`
}

type DatabaseConfig struct {
	Host               string `mapstructure:"host"`
	Port               int    `mapstructure:"port"`
	Username           string `mapstructure:"username"`
	Password           string `mapstructure:"password"`
	Name               string `mapstructure:"name"`
	SSLMode            string `mapstructure:"ssl_mode"`
	TimeZone           string `mapstructure:"timezone"`
	LogLevel           string `mapstructure:"log_level"`
	MaxOpenConns       int    `mapstructure:"max_open_conns"`
	MaxIdleConns       int    `mapstructure:"max_idle_conns"`
	MaxLifetime        int    `mapstructure:"max_lifetime"`
	MaxIdleTime        int    `mapstructure:"max_idle_time"`
	SlowQueryThreshold int    `mapstructure:"slow_query_threshold"` // 慢查询阈值（毫秒）
}

type LoggerConfig struct {
	Filename string `mapstructure:"filename"`
	Level    string `mapstructure:"level"`
}

type Config struct {
	Server   ServerConfig   `mapstructure:"server"`
	Database DatabaseConfig `mapstructure:"database"`
	Tools    ToolsConfig    `mapstructure:"tools"`
	Logger   *LoggerConfig  `mapstructure:"logger"`
}

type ToolsConfig struct {
	Postgres DatabaseConfig `mapstructure:"postgres"`
}

func LoadConfig() (*Config, error) {
	viper.SetConfigFile("./config.toml")
	err := viper.ReadInConfig()
	if err != nil {
		return nil, err
	}
	var config Config
	err = viper.Unmarshal(&config)
	if err != nil {
		return nil, err
	}

	return &config, nil
}
